//
//  CollectedViewController.m
//  cookBook
//
//  Created by xalo on 16/1/22.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "CollectedViewController.h"
#import "Model.h"
#import "WebViewController.h"
#import "CollectionHelper.h"
#import "RecordHelper.h"
#import "UserViewCell.h"
@interface CollectedViewController ()

@end

@implementation CollectedViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //添加清空
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清空" style:(UIBarButtonItemStylePlain) target:self action:@selector(deleteAll)];
    
    //注册单元格
    [self.tableView registerNib:[UINib nibWithNibName:@"UserViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell2"];
    
    //设置单元格高度
    self.tableView.rowHeight = 185;
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.collectionArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UserViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];

    Model *model = self.collectionArray[indexPath.row];
    
    [cell setCellWithModel:model];
    
    //block赋值
    __weak CollectedViewController *weakSelf = self;
    [cell finishLongPress:^{
        //弹出对话框
        [weakSelf presentAlertControllerWithIndexPath:indexPath];
        
    }];

    
    return cell;
}


//选中单元格
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //创建WebVC
    WebViewController *webVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"WebVC"];
    
    //传值到WebVC
    webVC.model = self.collectionArray[indexPath.row];
    
    //推出WebVC
    [self.navigationController pushViewController:webVC animated:YES];
}

#pragma mark -- Alert

//弹出提示框
- (void)presentAlertControllerWithIndexPath:(NSIndexPath *)indexPath
{
    //创建提示框
    UIAlertController *alerC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认删除" preferredStyle:(UIAlertControllerStyleActionSheet)];
    
    //添加确定按钮
    
    __weak CollectedViewController *weakSelf = self;
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        //删除当前的cell
        [weakSelf deleteCellWithIndexPath:indexPath];
        
        //提示框取消
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }];
    
    //添加取消按钮
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        //提示框取消
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [alerC addAction:action];
    [alerC addAction:action2];
    
    //推出提示框
    [self presentViewController:alerC animated:YES completion:nil];
}

#pragma mark -- 删除
- (void)deleteCellWithIndexPath:(NSIndexPath *)indexPath
{
    //判断进入浏览或收藏
    if (self.number == 0) {
        //找出model
        Model *model = self.collectionArray[indexPath.row];
        
        //删除数据库
        [[CollectionHelper sharedManager] deleteWithID:model.ID];
        
        //删除当前数组
        [self.collectionArray removeObjectAtIndex:indexPath.row];
        
        //刷新页面
        [self.tableView reloadData];
    }
    else{
        //找出model
        Model *model = self.collectionArray[indexPath.row];
        
        //删除数据库
        [[RecordHelper sharedManager] deleteWithID:model.ID];
        
        //删除当前数组
        [self.collectionArray removeObjectAtIndex:indexPath.row];
        
        //刷新页面
        [self.tableView reloadData];
    }
}


//删除全部
- (void)deleteAll
{
    //判断进入的是浏览记录或收藏
    if (self.number == 1) {
        //浏览记录
        
        
        //删除浏览数据库中数据
        [[RecordHelper sharedManager] deleteAll];
        
        //删除当前数组中数据
        [self.collectionArray removeAllObjects];
        
        //刷新界面
        [self.tableView reloadData];
    }else
    {
        //收藏
        
        
        //删除收藏数据库中内容
        [[CollectionHelper sharedManager] deleteAll];
        
        //删除当前数组中数据
        [self.collectionArray removeAllObjects];
        
        //刷新界面
        [self.tableView reloadData];
    }
    
    
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
